// pages/upConsultInfo/upConsultInfo.js
const app = getApp()
Page({
  data: {
    primaryColor: app.primaryColor, // 主题色
    datePickerShow: false, // 选择日期时间的弹框
    currentDate: null, // picker 选择框赋值的
    selectedTime: '',  // 当前选择的时间
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      } else if (type === 'month') {
        return `${value}月`;
      } else if (type === 'day') {
        return `${value}日`;
      } else if (type === 'hour') {
        return `${value}时`;
      } else if (type === 'minute') {
        return `${value}分`;
      }
      return value;
    },
    projectList: [
      {
        name: '挂号',
        value: 1,
        checked: false
      },
      {
        name: '处方',
        value: 2,
        checked: false
      },
      {
        name: '治疗',
        value: 3,
        checked: false
      },
      {
        name: '检验检查',
        value: 4,
        checked: false
      },
      {
        name: '住院',
        value: 5,
        checked: false
      },
      {
        name: '手术',
        value: 6,
        checked: false
      },
      {
        name: '护理',
        value: 7,
        checked: false
      },
      {
        name: '生育',
        value: 8,
        checked: false
      },
      {
        name: '其他',
        value: 9,
        checked: false
      }
    ],
    // 服务项目数据
    serveList: [
      {
        name: '血常规血清素',
        num: 2,
        unit: '次'
      },
      {
        name: '血清素推荐',
        num: 34,
        unit: '次'
      },
    ],
    // 是否执行
    radioRadios: [
      {value: 1, name: '是', checked: false},
      {value: 0, name: '否', checked: false},
    ]
  },
  onLoad(options) {

  },
  onShow() {
    this.initDateTime()
  },
  // 确定改变日期
  confirmDate(event) {
    const selectedDate = new Date(event.detail);
    this.setData({
      currentDate: event.detail,
      selectedTime: this.formatTime(selectedDate),
      datePickerShow: false
    })

  },
  // 关闭日期选择框
  cancelDate () {
    this.setData({ datePickerShow: false })
  },
  // 打开日期选择框
  chooseDate () {
    this.setData({
      datePickerShow: true
    })
  },
  // 初始化默认时间
  initDateTime() {
    let timestamp;

    if (this.data.selectedTime) {
      // 有默认值时，转换为时间戳
      timestamp = new Date(this.data.selectedTime).getTime();
    } else {
      // 无默认值时，使用当前时间戳
      timestamp = new Date().getTime();
    }

    // 更新数据
    this.setData({
      currentDate: timestamp,
      selectedTime: this.formatTime(new Date(timestamp))
    });
  },

  // 格式化时间为 "YYYY-MM-DD HH:MM"
  formatTime(date) {
    const year = date.getFullYear();
    const month = this.padZero(date.getMonth() + 1);
    const day = this.padZero(date.getDate());
    const hour = this.padZero(date.getHours());
    const minute = this.padZero(date.getMinutes());
    return `${year}/${month}/${day} ${hour}:${minute}`;
  },

  // 补零函数
  padZero(n) {
    return n < 10 ? `0${n}` : n;
  },
  // 就诊项目选择
  checkboxChange (e) {
    const projectList = this.data.projectList
    const values = e.detail.value
    for (let i = 0, lenI = projectList.length; i < lenI; ++i) {
      projectList[i].checked = false

      for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
        if (projectList[i].value == values[j]) {
          projectList[i].checked = true
          break
        }
      }
    }

    this.setData({
      projectList,
      isShow_itInfo: values.some(val => val == 9) ? true : false
    })
  },
  // 是否执行选择
  radioChange (e) {
    const radioRadios = this.data.radioRadios
    for (let i = 0, len = radioRadios.length; i < len; ++i) {
      radioRadios[i].checked = radioRadios[i].value == e.detail.value
    }
    this.setData({
      radioRadios
    })
  },
})